package com.wcl.nullmusic.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.wcl.nullmusic.R;
import com.wcl.nullmusic.mode.Anthology_Track_List;
import com.wcl.nullmusic.utils.NumberUtils;
import com.wcl.nullmusic.utils.BaseUtils;

import java.util.List;

/**
 * Created by dllo on 16/1/16.
 */
public class
        AnthologyListViewAdapter extends BaseAdapter {
    private List<Anthology_Track_List> datas;
    private String title;

    public void addData(List<Anthology_Track_List> datas, String title) {
        this.title = title;
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return datas != null && datas.size() > 0 ? datas.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (datas != null && datas.size() > 0) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.anthology_item, parent, false);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            Anthology_Track_List list = datas.get(position);
            if (title == null)
                holder.anthology_item_desc.setVisibility(View.GONE);
            else
                holder.anthology_item_desc.setText(list.getComments() + "");
            holder.anthology_item_title.setText(list.getTitle());
            holder.anthology_item_playerCounts.setText(NumberUtils.toUnit(list.getPlaytimes()));
            holder.anthology_item_duration.setText(BaseUtils.toDurtion2((int) list.getDuration()));


            return convertView;
        }
        return null;
    }

    class ViewHolder {
        private TextView anthology_item_title, anthology_item_playerCounts, anthology_item_duration, anthology_item_desc;

        public ViewHolder(View view) {
            anthology_item_title = (TextView) view.findViewById(R.id.anthology_item_title);
            anthology_item_playerCounts = (TextView) view.findViewById(R.id.anthology_item_playerCounts);
            anthology_item_duration = (TextView) view.findViewById(R.id.anthology_item_duration);
            anthology_item_desc = (TextView) view.findViewById(R.id.anthology_item_desc);
        }
    }
}
